//
//  DYWebViewController.m
//  BaiSi
//
//  Created by mac on 16/10/22.
//  Copyright © 2016年 DeYi. All rights reserved.
//

#import "DYWebViewController.h"
#import <WebKit/WebKit.h>

@interface DYWebViewController ()
@property (weak, nonatomic) IBOutlet UIView *webView;
@property (weak, nonatomic) IBOutlet UIBarButtonItem *goBackBtn;
@property (weak, nonatomic) IBOutlet UIBarButtonItem *goForwardBtn;
@property(nonatomic,weak) WKWebView *wkWebView;
@property (weak, nonatomic) IBOutlet UIProgressView *progressView;

@end

@implementation DYWebViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    
    //设置WebView
    [self setUpWebView];
    
    //监听
    [self addObserver];
}
#pragma mark - 添加监听者
- (void)addObserver {
    //前进,后退
    [_wkWebView addObserver:self forKeyPath:@"canGoBack" options:NSKeyValueObservingOptionNew context:nil];
    [_wkWebView addObserver:self forKeyPath:@"canGoForward" options:NSKeyValueObservingOptionNew context:nil];
    //主题
    [_wkWebView addObserver:self forKeyPath:@"title" options:NSKeyValueObservingOptionNew context:nil];
    //进度条
    [_wkWebView addObserver:self forKeyPath:@"estimatedProgress" options:NSKeyValueObservingOptionNew context:nil];
}
#pragma mark - 移除监听者
- (void)dealloc {
    
    [_wkWebView removeObserver:self forKeyPath:@"canGoBack"];
    [_wkWebView removeObserver:self forKeyPath:@"canGoForward"];
    [_wkWebView removeObserver:self forKeyPath:@"title"];
    [_wkWebView removeObserver:self forKeyPath:@"estimatedProgress"];
}

#pragma mark - 监听结果
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary<NSString *,id> *)change context:(void *)context {
    _goBackBtn.enabled = _wkWebView.canGoBack;
    _goForwardBtn.enabled = _wkWebView.canGoForward;
    _progressView.progress = _wkWebView.estimatedProgress;
    self.navigationItem.title = _wkWebView.title;
    _progressView.hidden = _progressView.progress >= 1;
//    LHLog(@"%@",NSStringFromCGRect(self.wkWebView.frame));
//    LHLog(@"%@",NSStringFromCGRect(self.view.frame));
}

#pragma mark - WKWebView点击事件
- (IBAction)goBackClick:(UIBarButtonItem *)sender {
    [_wkWebView goBack];
}
- (IBAction)goFowordClick:(UIBarButtonItem *)sender {
  
    [_wkWebView goForward];
   
}
- (IBAction)reloadClick:(UIBarButtonItem *)sender {
    [_wkWebView reload];
    
}

#pragma mark - 设置WKWebView
- (void)setUpWebView {
    //加载WKWebView
    WKWebView *webView = [[WKWebView alloc] initWithFrame:CGRectMake(0, 0, DYScreenW, DYScreenH - 44)];
    [_webView addSubview:webView];
    _wkWebView = webView;
//    _wkWebView.scrollView.contentInset = UIEdgeInsetsMake(0, 0, 44, 0);

    _wkWebView.scrollView.contentMode = UIViewContentModeScaleAspectFit;
    NSURLRequest *request = [NSURLRequest requestWithURL:_url];
    [webView loadRequest:request];
    
    //初始化前进,后退
//    _goBackBtn.enabled = NO;
//    _goForwardBtn.enabled = NO;
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
